﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using static CanTIceCream.HttpClient;

namespace CanTIceCream
{
    public class IceMsg
    {
        private static IceMsg instance;
        private IceMsg() { }
        public static IceMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IceMsg();
                }
                return instance;
            }
        }

        #region socket服务器
        public static bool status_coffee = true;
        private static byte[] result = new byte[1024];
        private static int myProt = 9527;  //端口 
        static Socket serverSocket;
        public bool Ismake = true;
        public bool IsmakeTrue = false;
        /// <summary>
        /// 服务器启动监听各种信息动态
        /// </summary>
        public void CoffeeMake()
        {
            try
            {
                string LocalIP = "192.168.0.77";
                var host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (var ips in host.AddressList)
                {
                    if (ips.AddressFamily == AddressFamily.InterNetwork)
                    {
                        LocalIP = ips.ToString();
                        // Console.WriteLine"IP Address = " + (ips.ToString());
                    }
                }
                //服务器IP地址 
                IPAddress ip = IPAddress.Parse(LocalIP);
                Console.WriteLine(LocalIP);
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                serverSocket.Bind(new IPEndPoint(ip, myProt)); //绑定IP地址：端口 
                serverSocket.Listen(10);  //设定最多10个排队连接请求 ab
                Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());
                //通过Clientsoket发送数据 
                Thread myThread = new Thread(ListenClientConnect);
                myThread.Start();
                Console.ReadLine();
            }
            catch (Exception)
            {
                return;
            }
        }
        List<Socket> clientSocketsList = new List<Socket>();
        public Socket clientsockets = null;

        /// <summary> 
        /// 监听客户端连接 
        /// </summary> 
        private void ListenClientConnect()
        {
            try
            {
                while (true)
                {
                    Socket clientSocket = serverSocket.Accept();
                    clientSocketsList.Add(clientSocket);
                    clientsockets = clientSocket;
                    ///  BtnTrue();
                    // SendMessage(name,clientSocket);
                    // clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));
                    Thread receiveThread = new Thread(ReceiveMessage);
                    receiveThread.IsBackground = true;
                    receiveThread.Start(clientSocket);
                }
            }
            catch (Exception)
            {
                return;
            }

        }
        public void ClientSend(string msg)
        {
            try
            {
                msg = msg.Replace(" ", "").Replace("\r\n", "");
                //将输入的16进制字符串两两分割为字符串集合
                var strArr = Regex.Matches(msg, ".{2}").Cast<Match>().Select(m => m.Value);
                //需要发送的字节数组
                byte[] data = new byte[strArr.Count()];
                //循环索引
                int temp = 0;
                //将字符串集合转换为字节数组
                foreach (string item in strArr)
                {
                    data[temp] = Convert.ToByte(item, 16);
                    temp++;
                }
                clientSocketsList[0].Send(data);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary> 
        /// 接收消息 
        /// </summary> 
        /// <param name="clientSocket"></param> 
        private void ReceiveMessage(object clientSockets)
        {
            Socket myClientSocket = (Socket)clientSockets;
            while (true)
            {
                try
                {
                    //通过clientSocket接收数据 
                    int receiveNumber = myClientSocket.Receive(result);
                    if (receiveNumber == 0) continue;
                    var dd = "";
                    var dd1 = Encoding.UTF8.GetString(result, 0, receiveNumber);
                    var dd2 = Encoding.ASCII.GetString(result, 0, receiveNumber);
                    for (int i = 0; i < receiveNumber; i++)
                    {
                        dd += $"{result[i].ToString("X2")} ";
                    }
                    MsgShow(dd);
                    Console.WriteLine(dd + " " + DateTime.Now.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }
            }

        }
        public void MsgShow(string str)
        {
            try
            {
                var list = str.Split(' ');
                if (list.Count() > 10)
                {
                    if (list[0] == "AA")
                    {
                        if (list[3] == "00")
                        {
                            Console.WriteLine(str + " 00 " + DateTime.Now.ToString());
                            SelShow(list);
                        }
                        else if (list[3] == "02")
                        {
                            Ismake = false;
                            if (list[9] == "01")
                            {
                                IsmakeTrue = true;
                            }
                        }
                        else if (list[3] == "03")
                        {
                            Console.WriteLine(str + " 03 " + DateTime.Now.ToString());
                            SelShow(list);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void SelShow(string[] strs)
        {
            try
            {
                if (strs.Count() >= 34)
                {
                    IceModel.Instance.IsKZMS = strs[8];
                    Console.WriteLine(IceModel.Instance.IsKZMS + " 控制模式" + DateTime.Now.ToString());
                    IceModel.Instance.IsGZZT = strs[10];
                    Console.WriteLine(IceModel.Instance.IsGZZT + " 工作状态" + DateTime.Now.ToString());
                    IceModel.Instance.IsSBZT = strs[12];
                    Console.WriteLine(IceModel.Instance.IsSBZT + " 设备状态" + DateTime.Now.ToString());
                    IceModel.Instance.IsQCKZT = strs[14];
                    Console.WriteLine(IceModel.Instance.IsQCKZT + " 取餐口状态" + DateTime.Now.ToString());
                    if (IceModel.Instance.IsSBZTMXBJ != (strs[17] + strs[16]))
                    {
                        var bj = strs[17] + strs[16];
                        if (bj.Length == 4)
                        {
                            BJShow(bj);
                        }
                    }
                    IceModel.Instance.IsSBZTMXBJ = strs[17] + strs[16];
                    if (IceModel.Instance.IsSBZTMXGZ != (strs[19] + strs[18]))
                    {
                        var bj = strs[19] + strs[18];
                        if (bj.Length == 4)
                        {
                            GZShow(bj);
                        }
                    }
                    IceModel.Instance.IsSBZTMXGZ = strs[19] + strs[18];
                    Console.WriteLine(IceModel.Instance.IsSBZTMXGZ + " 设备状态明细（故障）" + DateTime.Now.ToString());
                    IceModel.Instance.IsDQLCZT = strs[20];
                    Console.WriteLine(IceModel.Instance.IsDQLCZT + " 当前流程状态" + DateTime.Now.ToString());
                    IceModel.Instance.IsXGJBTZT = strs[22];
                    Console.WriteLine(IceModel.Instance.IsXGJBTZT + " 雪糕机本体状态" + DateTime.Now.ToString());
                    IceModel.Instance.IsDQCXBFB = strs[24];
                    Console.WriteLine(IceModel.Instance.IsDQCXBFB + " 当前成型百分比" + DateTime.Now.ToString());
                    IceModel.Instance.IsDQDLCLSJ = strs[26];
                    Console.WriteLine(IceModel.Instance.IsDQDLCLSJ + " 当前定量出料时间" + DateTime.Now.ToString());
                    IceModel.Instance.IsDQQLSYXGS = strs[28];
                    Console.WriteLine(IceModel.Instance.IsDQQLSYXGS + " 当前缺料允许个数" + DateTime.Now.ToString());
                    IceModel.Instance.IsDQLJCLDGS = strs[30];
                    Console.WriteLine(IceModel.Instance.IsDQLJCLDGS + " 当前累计个数" + DateTime.Now.ToString());
                    IceModel.Instance.IsFWWCBZ = strs[32];
                    Console.WriteLine(IceModel.Instance.IsFWWCBZ + " 复位完成标记" + DateTime.Now.ToString());
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// //16转2方法
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static string HexString2BinString(string hexString)
        {
            try
            {
                string result = string.Empty;
                foreach (char c in hexString)
                {
                    int v = Convert.ToInt32(c.ToString(), 16);
                    int v2 = int.Parse(Convert.ToString(v, 2));
                    // 去掉格式串中的空格，即可去掉每个4位二进制数之间的空格，
                    result += string.Format("{0:d4}", v2);
                }
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }

        }
        public void BJShow(string str)
        {
            try
            {
                var bj = HexString2BinString(str);
                if (bj.Length == 16)
                {
                    bool bol = false;
                    bool ErrorBol = false;
                    IceBJ.Instance.IsGKQCCS = bj.Substring(bj.Length - 1, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsGKQCCS + " 顾客取餐超时" + DateTime.Now.ToString());
                    if (IceBJ.Instance.IsXGJCL != ((bj.Substring(bj.Length - 2, 1) == "1") ? false : true))
                    {
                        iceMetters.Instance.IceMatter9 = (bj.Substring(bj.Length - 2, 1) == "1") ? 0 : 1;
                        Console.WriteLine("queliao " + iceMetters.Instance.IceMatter9);
                        if (iceMetters.Instance.IceMatter9 == 0)
                            ErrorBol = true;
                        bol = true;
                    }
                    IceBJ.Instance.IsXGJCL = bj.Substring(bj.Length - 2, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsXGJCL + " 雪糕机缺料" + DateTime.Now.ToString());
                    if (IceBJ.Instance.IsLBQQB1 != ((bj.Substring(bj.Length - 3, 1) == "1") ? false : true))
                    {

                        iceMetters.Instance.IceMatter1 = ((bj.Substring(bj.Length - 3, 1) == "1") && (bj.Substring(bj.Length - 4, 1) == "1")) ? 0 : 1;
                        if (iceMetters.Instance.IceMatter1 == 0)
                            ErrorBol = true;
                        bol = true;
                    }
                    IceBJ.Instance.IsLBQQB1 = bj.Substring(bj.Length - 3, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsLBQQB1 + " 1号落杯器缺杯" + DateTime.Now.ToString());
                    if (IceBJ.Instance.IsLBQQB2 != ((bj.Substring(bj.Length - 4, 1) == "1") ? false : true))
                    {
                        iceMetters.Instance.IceMatter1 = ((bj.Substring(bj.Length - 3, 1) == "1") && (bj.Substring(bj.Length - 4, 1) == "1")) ? 0 : 1;
                        if (iceMetters.Instance.IceMatter1 == 0)
                            ErrorBol = true;
                        bol = true;
                    }
                    IceBJ.Instance.IsLBQQB2 = bj.Substring(bj.Length - 4, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsLBQQB2 + " 2号落杯器缺杯" + DateTime.Now.ToString());
                    if (IceBJ.Instance.IsYJGYBJ != ((bj.Substring(bj.Length - 5, 1) == "1") ? false : true))
                    {
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 5, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceBJ.Instance.IsYJGYBJ = bj.Substring(bj.Length - 5, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsYJGYBJ + " 雪糕机压机预警" + DateTime.Now.ToString());
                    //IceBJ.Instance.IsGSQL1 = bj.Substring(bj.Length - 6, 1) == "1" ? true : false;
                    //Console.WriteLine(IceBJ.Instance.IsGSQL1 + " 果碎1缺料" + DateTime.Now.ToString());
                    //IceBJ.Instance.IsGSQL2 = bj.Substring(bj.Length - 7, 1) == "1" ? true : false;
                    //Console.WriteLine(IceBJ.Instance.IsGSQL2 + " 果碎2缺料" + DateTime.Now.ToString());
                    //IceBJ.Instance.IsGSQL3 = bj.Substring(bj.Length - 8, 1) == "1" ? true : false;
                    //Console.WriteLine(IceBJ.Instance.IsGSQL3 + " 果碎3缺料" + DateTime.Now.ToString());
                    if (IceBJ.Instance.IsGJQL1 != ((bj.Substring(bj.Length - 9, 1) == "1") ? false : true))
                    {
                        iceMetters.Instance.IceMatter5 = ((bj.Substring(bj.Length - 9, 1) == "1")) ? 0 : 1;
                        bol = true;
                    }
                    IceBJ.Instance.IsGJQL1 = bj.Substring(bj.Length - 9, 1) == "1" ? true : false;

                    Console.WriteLine(IceBJ.Instance.IsGJQL1 + " 果酱1缺料" + DateTime.Now.ToString());
                    if (IceBJ.Instance.IsGJQL2 != ((bj.Substring(bj.Length - 10, 1) == "1") ? false : true))
                    {
                        iceMetters.Instance.IceMatter6 = ((bj.Substring(bj.Length - 10, 1) == "1")) ? 0 : 1;
                        bol = true;
                    }
                    IceBJ.Instance.IsGJQL2 = bj.Substring(bj.Length - 10, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsGJQL2 + " 果酱2缺料" + DateTime.Now.ToString());
                    if (IceBJ.Instance.IsGJQL3 != ((bj.Substring(bj.Length - 11, 1) == "1") ? false : true))
                    {
                        iceMetters.Instance.IceMatter7 = ((bj.Substring(bj.Length - 11, 1) == "1")) ? 0 : 1;
                        bol = true;
                    }
                    IceBJ.Instance.IsGJQL3 = bj.Substring(bj.Length - 11, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsGJQL3 + " 果酱3缺料" + DateTime.Now.ToString());
                    IceBJ.Instance.IsMWG = bj.Substring(bj.Length - 13, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsMWG + " 门未关" + DateTime.Now.ToString());
                    IceBJ.Instance.IsHYSB = bj.Substring(bj.Length - 14, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsHYSB + " 回原失败" + DateTime.Now.ToString());
                    IceBJ.Instance.IsDCPZT = bj.Substring(bj.Length - 15, 1) == "1" ? true : false;
                    Console.WriteLine(IceBJ.Instance.IsDCPZT + " 点餐屏状态" + DateTime.Now.ToString());
                    if (bol)
                    {
                        var ss = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\报警\";
                        File.WriteAllText(ss + DateTime.Now.ToString("yyyyddMMHHmmss") + "queliao.txt", bj);
                        saveDeviceMatter save = new saveDeviceMatter();
                        save.matterStatusJson = JsonConvert.SerializeObject(iceMetters.Instance);
                        save.deviceStatusJson = JsonConvert.SerializeObject(deviceStatus.Instance);
                        var saves = JsonConvert.SerializeObject(save);
                        var dd = HttpMsg.Instance.saveDeviceMatterMsg(saves);
                        Console.WriteLine(saves);
                        if (ErrorBol)
                        {
                            EmailShow emailShow = new EmailShow();
                            emailShow.content = MainWindow.EmailError(iceMetters.Instance.IceMatter9, iceMetters.Instance.IceMatter1);
                            var saves1 = JsonConvert.SerializeObject(emailShow);
                            var dd1 = HttpMsg.Instance.sendEmailMsg(saves1);
                        }
                    }
                }
            }
            catch (Exception)
            {
                var ss = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\报警\";
                File.WriteAllText(ss + DateTime.Now.ToString("yyyyddMMHHmmss") + "queliao.txt", str);
                return;
            }
        }
        public void GZShow(string str)
        {
            try
            {
                var bj = HexString2BinString(str);
                if (bj.Length == 16)
                {
                    bool bol = false;
                    string BJMsg = "";
                    IceGJ.Instance.IsLBQWB1 = bj.Substring(bj.Length - 1, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsLBQWB1 + " 1号落杯器无杯" + DateTime.Now.ToString());
                    IceGJ.Instance.IsLBQWB2 = bj.Substring(bj.Length - 2, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsLBQWB2 + " 2号落杯器无杯" + DateTime.Now.ToString());
                    IceGJ.Instance.IsLBQWBALL = bj.Substring(bj.Length - 3, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsLBQWBALL + " 落杯器无杯" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsWJZLZT != ((bj.Substring(bj.Length - 4, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 4, 1) == "1") ? false : true))
                            BJMsg = "未进入制冷状态";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 4, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsWJZLZT = bj.Substring(bj.Length - 4, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsWJZLZT + " 未进入制冷状态" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsXGJWL != ((bj.Substring(bj.Length - 5, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 5, 1) == "1") ? false : true))
                            BJMsg = "雪糕机无料";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 5, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsXGJWL = bj.Substring(bj.Length - 5, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsXGJWL + " 雪糕机无料" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsXGJGZ != ((bj.Substring(bj.Length - 6, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 6, 1) == "1") ? false : true))
                            BJMsg = "雪糕机故障";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 6, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsXGJGZ = bj.Substring(bj.Length - 6, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsXGJGZ + " 雪糕机故障" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsXGJDX != ((bj.Substring(bj.Length - 7, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 7, 1) == "1") ? false : true))
                            BJMsg = "雪糕机掉线";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 7, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsXGJDX = bj.Substring(bj.Length - 7, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsXGJDX + " 雪糕机掉线" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsXGJCLCS != ((bj.Substring(bj.Length - 8, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 8, 1) == "1") ? false : true))
                            BJMsg = "雪糕机出料超时";
                        //deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 8, 1) == "1" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsXGJCLCS = bj.Substring(bj.Length - 8, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsXGJCLCS + " 雪糕机出料超时" + DateTime.Now.ToString());
                    IceGJ.Instance.IsGSYZAW = bj.Substring(bj.Length - 9, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsGSYZAW + " 光栅故障" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsDZYXZWBZ != ((bj.Substring(bj.Length - 11, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 11, 1) == "1") ? false : true))
                            BJMsg = "电爪运行中无杯子";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 11, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsDZYXZWBZ = bj.Substring(bj.Length - 11, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsDZYXZWBZ + " 电爪运行中无杯子" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsXZTXZDJBJ != ((bj.Substring(bj.Length - 12, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 12, 1) == "1") ? false : true))
                            BJMsg = "旋转台电机报警";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 12, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsXZTXZDJBJ = bj.Substring(bj.Length - 12, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsXZTXZDJBJ + " 旋转台电机报警" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsXZTSJDJBJ != ((bj.Substring(bj.Length - 13, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 13, 1) == "1") ? false : true))
                            BJMsg = "旋转台升降电机报警";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 13, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsXZTSJDJBJ = bj.Substring(bj.Length - 13, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsXZTSJDJBJ + " 旋转台升降电机报警" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsMSJDJBJ != ((bj.Substring(bj.Length - 14, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 14, 1) == "1") ? false : true))
                            BJMsg = "门升降电机报警";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 14, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsMSJDJBJ = bj.Substring(bj.Length - 14, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsMSJDJBJ + " 门升降电机报警" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsCKBZXHSB != ((bj.Substring(bj.Length - 15, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 15, 1) == "1") ? false : true))
                            BJMsg = "出餐口杯子下滑失败";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 15, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsCKBZXHSB = bj.Substring(bj.Length - 15, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsCKBZXHSB + " 出餐口杯子下滑失败" + DateTime.Now.ToString());
                    if (IceGJ.Instance.IsXZTXZCS != ((bj.Substring(bj.Length - 16, 1) == "1") ? false : true))
                    {
                        if (!((bj.Substring(bj.Length - 16, 1) == "1") ? false : true))
                            BJMsg = "旋转台旋转超时";
                        deviceStatus.Instance.deviceStatus3 = bj.Substring(bj.Length - 16, 1) == "0" ? 1 : 0;
                        bol = true;
                    }
                    IceGJ.Instance.IsXZTXZCS = bj.Substring(bj.Length - 16, 1) == "1" ? true : false;
                    Console.WriteLine(IceGJ.Instance.IsXZTXZCS + " 旋转台旋转超时" + DateTime.Now.ToString());
                    if (bol)
                    {
                        var ss = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\报警\";
                        File.WriteAllText(ss + DateTime.Now.ToString("yyyyddMMHHmmss") + ".txt", bj);
                        saveDeviceMatter save = new saveDeviceMatter();
                        save.matterStatusJson = JsonConvert.SerializeObject(iceMetters.Instance);
                        save.deviceStatusJson = JsonConvert.SerializeObject(deviceStatus.Instance);
                        var saves = JsonConvert.SerializeObject(save);
                        var dd = HttpMsg.Instance.saveDeviceMatterMsg(saves);
                        Console.WriteLine(saves);
                        if (BJMsg != "")
                        {
                            EmailShow emailShow = new EmailShow();
                            emailShow.content = EmailError(BJMsg);
                            var saves1 = JsonConvert.SerializeObject(emailShow);
                            var dd1 = HttpMsg.Instance.sendEmailMsg(saves1);
                        }
                    }
                }
            }
            catch (Exception)
            {
                var ss = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\报警\";
                File.WriteAllText(ss + DateTime.Now.ToString("yyyyddMMHHmmss") + ".txt", str);
                return;
            }
        }
        public static string EmailError(string bql)
        {
            try
            {
                string msg = "<style>table{ border: 1px solid black; border-collapse: collapse; }table td{border: 1px solid #000000; padding: 5px 15px;font-size:14px;}</style><table>";
                msg += "<tr><td style='font-weight:bold;'>冰淇淋</td><td style='color:#FF0000;'>" + bql + "</td></tr>";
                msg += "</table>";
                return msg;
            }
            catch (Exception)
            {
                return "";
            }
        }
        public static Socket clientSocketes;
        #endregion
    }
}
